import path
import requests
import os
import TEST.Head.Qfunc as Qfunc
from PIL import Image

PATH = path.Path()
PATH.fp = path.now()
PATH.back()

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.9 Safari/537.36',
}
def Walefile(file):
    for root,dirs,files in os.walk(file):
        for f in files:
            name = f[:f.find('.')]
            # 新建文件夹
            PATH.isHere(name)
            # 获取记录内容
            with open(f, 'r') as ff:
                urls = ff.read().split('\n')
            # 逐页读写
            for j in range(len(urls)-1):
                # 预处理
                print(urls[j])
                fp = name + '/' + str(j)
                fp_webp = fp + '.webp'

                # 下载图片
                res = requests.get(urls[j], headers=headers)

                with open(fp_webp, 'wb') as ff:
                    for chunk in res.iter_content(100000):
                        ff.write(chunk)

                Qfunc.wait(1)
                # 图片转码
                img = Image.open(fp_webp)
                img.load()
                img.save(fp + '.jpg')

                os.remove(fp_webp)

Walefile('.')